package com.edgclub.edg.activity;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.edgclub.edg.Constant;
import com.edgclub.edg.R;
import com.edgclub.edg.event.BroadCastEvent;
import com.edgclub.edg.event.EventBus;
import com.edgclub.edg.model.User;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.squareup.otto.ThreadEnforcer;

import java.util.HashMap;

/**
 * Created by Administrator on 2016/6/17.
 */
public class MyInforMationActivity extends GzBaseTooolBarActivity implements View.OnClickListener{
    private RelativeLayout mMy_icon_layout;
    private RelativeLayout mPerson_sing_layout;
    private RelativeLayout mPerson_name_layout;
    private RelativeLayout mPerson_album_layout;
    private ImageView my_person_icon;
    private GlideUtils glideUtils = new GlideUtils();;
    private UserPrefUtils userPrefUtils;
    private User user;
    private TextView sign_text , name_text;





    @Override
    public int getContentView() {
        return R.layout.activity_my_information;
    }


    @Override
    public void setTitleBarView() {
        setCustomActionBar();
        title.setText("我的资料");
        main_back.setVisibility(View.VISIBLE);
        main_back.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

    }

    @Override
    public void initView() {
        mMy_icon_layout = (RelativeLayout) findViewById(R.id.my_icon_layout);
        mPerson_sing_layout = (RelativeLayout) findViewById(R.id.person_sing_layout);
        mPerson_name_layout = (RelativeLayout) findViewById(R.id.person_name_layout);
        mPerson_album_layout = (RelativeLayout) findViewById(R.id.person_album_layout);
        my_person_icon = (ImageView) findViewById(R.id.my_person_icon);
        name_text = (TextView) findViewById(R.id.name_text);
        sign_text = (TextView) findViewById(R.id.sign_text);


        mMy_icon_layout.setOnClickListener(this);
        mPerson_sing_layout.setOnClickListener(this);
        mPerson_name_layout.setOnClickListener(this);
        mPerson_album_layout.setOnClickListener(this);

    }


    @Override
    public void initData() {
        userPrefUtils = new UserPrefUtils(this);
        user=userPrefUtils.getUser();
        if(null==user)user=new User();
        glideUtils.Glidde_loadCropCircleImage(this,userPrefUtils.getUser().getLogo(),my_person_icon);
        if ("".equals(userPrefUtils.getUser().getIntro())){
            sign_text.setText("警察蜀黍，就是这个人不写签名");
        }else{
            sign_text.setText(userPrefUtils.getUser().getIntro());
        }
        name_text.setText(userPrefUtils.getUser().getNick());

    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_back:
                Intent intent3=new Intent(MyInforMationActivity.this,GzMainActivity.class);
                intent3.putExtra("user", user);
                setResult(RESULT_OK, intent3);
                finish();
                break;
            case R.id.my_icon_layout:
                Intent intent2 = new Intent(MyInforMationActivity.this, MultiImageSelectorActivity.class);
                intent2.putExtra("selectType",1);
                // 是否显示拍摄图片
                intent2.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
                // 最大可选择图片数量
                intent2.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 1);
                // 选择模式
                intent2.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_SINGLE);
//                startActivity(intent2);
                startActivityForResult(intent2, Constant.IMAGE_RESULT);
                break;
            case R.id.person_sing_layout:
                Intent intent1 = new Intent(MyInforMationActivity.this,MyInformationChangeSignActivity.class);
                startActivityForResult(intent1, Constant.SING_RESULT);
                break;
            case R.id.person_name_layout:
                Intent intent = new Intent(MyInforMationActivity.this,MyInformationChangeNameActivity.class);
                startActivityForResult(intent, Constant.SING_RESULT);
                break;
            case R.id.person_album_layout:
                Intent intent4=new Intent(this,UserInfoOtherPhotoActivity.class);
                intent4.putExtra("user",user);
                startActivity(intent4);
                break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == Constant.IMAGE_RESULT) {
                String str=data.getStringExtra("userPath")+"";
                glideUtils.Glidde_loadCropCircleImage(MyInforMationActivity.this,str,my_person_icon);
                user.setLogo(str);
                userPrefUtils.saveUser(user);
                HashMap<String, Object> hashMap = new HashMap<String, Object>();
                hashMap.put("event", BroadCastEvent.USERICONUPLOAD);
                hashMap.put("user", user);
                EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON, ThreadEnforcer.MAIN).post(hashMap);
            }else if(requestCode==Constant.SING_RESULT){
                User user1= (User) data.getSerializableExtra("user");
                if(null!=user1) {
                    name_text.setText(user1.getNick());
                    if ("".equals(user1.getIntro())){
                        sign_text.setText("警察蜀黍，就是这个人不写签名!");
                    }else{
                        sign_text.setText(user1.getIntro());
                    }
                    user = user1;
                    userPrefUtils.saveUser(user);
                }
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent3=new Intent(MyInforMationActivity.this,GzMainActivity.class);
            intent3.putExtra("user", user);
            setResult(RESULT_OK, intent3);
            finish();
            return true;
        }
        return false;

    }
}
